/*
 * Project: SmartWard
 * Package: com.ruoyi.ichen.app.controller
 * File: ProfileUploadAndDownContronller
 * Date: 2020-2-16 22:51
 * Version: V1.0
 * Copyright © 2018 - 2019 YESOFT CHINA, Inc. All Rights Reserved.
 */
package com.ruoyi.ichen.app.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.ichen.app.service.IProfileService;
import com.ruoyi.ichen.app.service.ProfileUploadAndDownService;
import com.ruoyi.ichen.base.rest.RestDTO;
import com.ruoyi.ichen.entity.IProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @ClassName: ProfileUploadAndDownContronller
 * @Description: 文件上传和下载 todo 目前还没明确，先这样命名
 * @Author chenhuanghua
 * @Date 2020-2-16 22:51
 * @Version V1.0
 */
@RestController
@RequestMapping("/app/profile")
public class ProfileUploadAndDownController {

    @Autowired
    ProfileUploadAndDownService profileUploadAndDownService;
    @Autowired
    IProfileService profileService;

    /**
     * 图片上传
     * @param file
     * @return
     */
    @RequestMapping("/pic/upload")
    public RestDTO picUploadFile(@RequestParam("uploadFile") MultipartFile file){
        String uplod = profileUploadAndDownService.upload(file, MimeTypeUtils.IMAGE_EXTENSION);
        //保存数据库
        profileService.insertByUpload(uplod,file);
        if(StringUtils.isEmpty(uplod)){
            return new RestDTO(false,"上传失败，请联系管理员");
        }else{
            return new RestDTO(true,uplod);
        }
    }

    /**
     * 除了图片其余文件上传
     * @param file
     * @return
     */
    @RequestMapping("/other/upload")
    public RestDTO uploadFile(@RequestParam("uploadFile") MultipartFile file){
        //url
        String uplod = profileUploadAndDownService.upload(file, null);
        //保存数据库
        profileService.insertByUpload(uplod,file);
        if(StringUtils.isEmpty(uplod)){
            return new RestDTO(false,"上传失败，请联系管理员");
        }else{
            return new RestDTO(true,uplod);
        }
    }

    /**
     * 上传文件
     * @param profile
     * @return
     */
    @PostMapping("/add/profile")
    public RestDTO addProfile(@RequestBody IProfile profile){
        if(profile == null) return new RestDTO(false,"对象为空");
        profile.setCreateTime(new Date());
        profileService.insert(profile);

        return new RestDTO(true,"上传成功");
    }

}